﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate;

namespace Sealight.Data
{
   public class NewsDataRepository: NHibernateRepository<NewsEntity>, INewsDataRepository
    {
        #region INewsDataRepository Members

        public IList<NewsCategoryEntity> GetAllCategory()
        {
            IList<NewsCategoryEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(NewsCategoryEntity))
                    .List<NewsCategoryEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        public NewsCategoryEntity GetCategory(Guid id)
        {
            NewsCategoryEntity results = null;
            try
            {
                results = UnitOfWork.CurrentSession.Get<NewsCategoryEntity>(id);
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return results;
        }

        public IList<NewsSourceEntity> GetAllSource()
        {
            IList<NewsSourceEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(NewsSourceEntity))
                    .List<NewsSourceEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        public NewsSourceEntity GetSourceById(Guid id)
        {
            NewsSourceEntity results = null;
            try
            {
                results = UnitOfWork.CurrentSession.Get<NewsSourceEntity>(id);
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return results;
        }

        #endregion
    }
}
